<?php

class Mysql
{
	private static $_instance = null;
	
	private $_db = null;
	private $_dbhost = null;
	private $_dbuser = null;
	private $_dbpass = null;
	private $_dbname = null;
	private $_sql = null;
	private $_query = null;
	private $_lastInsertId = 0;
	
	private function __construct()
	{
	}
	
	public static function getInstance()
	{
		if(self::$_instance == null)
		{
			self::$_instance = new self;
		}
		
		return self::$_instance;
	}
	
	public function connect($dbhost, $dbuser, $dbpass)
	{
		$this->_dbhost = $dbhost;
		$this->_dbuser = $dbuser;
		$this->_dbpass = $dbpass;
		
		$this->_db = mysql_connect($this->_dbhost, $this->_dbuser, $this->_dbpass);
	}
	
	public function getLastInsertId()
	{
		return $this->_lastInsertId;
	}
	
	public function query($sql)
	{
		$this->_sql = $sql;
		
		$this->_query = mysql_query($sql) or $this->error(mysql_error());
		$this->_lastInsertId = mysql_insert_id();
		
		return $this->_query;
	}
	
	public function selectDB($dbname)
	{
		$this->_dbname = $dbname;
		
		return mysql_select_db($dbname, $this->_db);
	}
	
	public function error($message)
	{
		$out = <<<END
			{$message}<br />

END;
		echo $out;
		
		echo "<pre>";
		print_r(str_replace("\t", "", $this->_sql));
		echo "</pre>";
		
		die();
	}
}

?>